package ink.metoo.auto.fishpi.shell

import ink.metoo.auto.fishpi.Log
import ink.metoo.auto.fishpi.Settings
import ink.metoo.auto.fishpi.call.ChatRoomCall
import ink.metoo.auto.fishpi.chatroom.ChatRooms
import ink.metoo.auto.fishpi.chatroom.Message
import ink.metoo.auto.fishpi.chatroom.Terminals
import ink.metoo.auto.fishpi.chatroom.command.CommandFactory

object ShellMain : Runnable {

    override fun run() {

        while (true) {
            val content = Terminals.lineReader.readLine().trim()
            if (content.isBlank()) {
                continue
            }
            if (content.startsWith("/")) {
                CommandFactory.handle(Message().apply {
                    this.content = content
                    this.md = content
                    this.type = "local"
                    this.userName = Settings.fishpiClient.username
                })
                continue
            }
            try {
                val result = ChatRoomCall.sendMessage(content)
                if (result.code != 0) {
                    throw RuntimeException(result.msg)
                }
            } catch (e: Exception) {
                ChatRooms.console(content = "消息发送失败 errMsg: ${e.message}")
                Log.error(e.message, e)
            }

        }
    }

}

fun main() {
    ShellMain.run()
}

